public class Sarsa
{

	public Sarsa()
	{

	}

	public static void main(String[] arg)
	{

		Sarsa sarsa = new Sarsa();
		int numBatches = 1;
		int numberEpisodes = 200;
		double avePerBatch = 0;
		double aveAtEachStep[] = new double[numberEpisodes];
		for (int j = 0; j < numBatches; j++)
		{
			sarsa.init();
			for (int i = 0; i < numberEpisodes; i++)
			{
				sarsa.run();
				SuperLearn.writeF("graph.csv");
			}
		}
	}

	private void init()
	{
		SuperLearn.reset();
	}

	/*
	 * This method runs one episode of the Party
	 */

	private double run()
	{
		double ret = 0;
		double[] state = MountainCar.init();
		double[] nextState = null;
		int action;
		int count=0;
		while(state != null){
			//System.out.println("posistion = "+state[0]+ " velocity = " +state[1]);
			action = maxAction(state);
			nextState = MountainCar.transition(state, action);
			if(nextState != null){
				SuperLearn.learn(action, state,nextState,-1);
			}
			state = nextState;
			count++;
		}
		System.out.println(count);
		return ret;
	}

	private int maxAction(double [] state){
		int action = 0;
		double q0 = SuperLearn.f(0,state);
		double q1 = SuperLearn.f(1,state);
		double q2 = SuperLearn.f(2,state);
		if(q0<q1){
			action = 1;
			if(q1<q2){
				action = 2;
			}
		}
		else if(q0<q2){
			action = 2;
		}
		return action;
	}
}
